/*
 * Copyright 2015 Zhongan.com All right reserved. This software is the
 * confidential and proprietary information of Zhongan.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Zhongan.com.
 */
package com.zhongan.gateway.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zhongan.gateway.bean.Api;
import com.zhongan.gateway.bean.ApiPrivilege;
import com.zhongan.gateway.common.enums.ResponseCodeEnum;
import com.zhongan.gateway.common.exception.GatewayException;
import com.zhongan.gateway.service.ApiPrivilegeService;
import com.zhongan.gateway.service.ApiService;
import com.zhongan.gateway.web.common.BaseController;
import com.zhongan.gateway.web.common.ResultBase;

/***
 * 测试用的Controller
 * 
 * @author
 */
@Controller
@RestController
@Configuration
@RequestMapping("/api")
public class ApiPrivilegeController extends BaseController {

    //    private Logger logger = LoggerFactory.getLogger(TestController.class);

    @Resource
    private ApiPrivilegeService apiPrivilegeService;

    @Resource
    private ApiService          apiService;

    @RequestMapping("/deletePrivilegeById")
    ResultBase<Object> deletePrivilegeById(ApiPrivilege record) throws GatewayException {
        if (record == null || record.getId() == null || record.getApiId() == null || record.getDeveloperId() == null) {
            throw new GatewayException(ResponseCodeEnum.PARAM_FAIL.getCode(), ResponseCodeEnum.PARAM_FAIL.getMsg());
        }
        int i = apiPrivilegeService.deletePrivilegeById(record);
        return getSucResultBase(i);
    }

    @RequestMapping("/insertPrivilege")
    ResultBase<Object> insertPrivilege(ApiPrivilege apiPrivilege) {

        int i = apiPrivilegeService.insertPrivilege(apiPrivilege);
        return getSucResultBase(i);
    }

    @RequestMapping("/updatePrivilege")
    ResultBase<Object> updatePrivilege(ApiPrivilege apiPrivilege) {

        int i = apiPrivilegeService.updatePrivilege(apiPrivilege);
        return getSucResultBase(i);
    }

    @RequestMapping("/getPriById")
    ResultBase<Object> getPriById(Long id) {

        ApiPrivilege apiPrivilege = apiPrivilegeService.selectPriById(id);
        return getSucResultBase(apiPrivilege);
    }

    @RequestMapping("/getPrivileges")
    ResultBase<Object> getPrivileges(Long developerId) {

        List<ApiPrivilege> list = apiPrivilegeService.selectPrivileges(developerId);
        ResultBase<Object> obj = null;
        if (null != list && !list.isEmpty()) {
            List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(list.size());
            for (ApiPrivilege ap : list) {

                Api api = apiService.selectApiById(ap.getApiId());
                if (null == api) {
                    apiPrivilegeService.deletePrivilegeById(ap);
                } else {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("apiId", api.getId());
                    map.put("name", api.getName());
                    map.put("serviceName", api.getServiceName());
                    map.put("version", api.getVersion());
                    map.put("throttledCntBySec", ap.getThrottledCntBySec());
                    map.put("id", ap.getId());
                    result.add(map);
                }

            }
            obj = getSucResultBase(result);
        } else {
            obj = getSucResultBase(list);
        }
        return obj;
    }
}
